<template>
  <div class="home">
    <!-- 用户信息展示 -->
    <div v-if="userProfile" class="user-profile">
      <img :src="userProfile.avatar" :alt="userProfile.username" />
      <h2>{{ userProfile.username }}</h2>
      <p>{{ userProfile.bio }}</p>
    </div>

    <!-- 文章列表展示 -->
    <div class="article-list">
      <div v-for="article in articles" :key="article.id" class="article-item">
        <img :src="article.coverImage" :alt="article.title" />
        <div class="article-content">
          <h3>{{ article.title }}</h3>
          <p>{{ article.description }}</p>
          <div class="article-meta">
            <span>作者：{{ article.author }}</span>
            <span>发布日期：{{ article.date }}</span>
            <span>阅读量：{{ article.readCount }}</span>
          </div>
          <div class="article-tags">
            <span v-for="(tag, index) in article.tags" :key="index" class="tag">
              {{ tag }}
            </span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import axios from "axios";

export default {
  name: "Home",
  data() {
    return {
      articles: [],
      userProfile: null,
    };
  },
  created() {
    this.fetchArticles();
    this.fetchUserProfile();
  },
  methods: {
    async fetchArticles() {
      try {
        const response = await axios.get("/api/articles");
        this.articles = response.data.data;
      } catch (error) {
        console.error("获取文章列表失败:", error);
      }
    },
    async fetchUserProfile() {
      try {
        const response = await axios.get("/api/user/profile");
        this.userProfile = response.data.data;
      } catch (error) {
        console.error("获取用户信息失败:", error);
      }
    },
  },
};
</script>

<style scoped>
.home {
  padding: 20px;
}

.user-profile {
  text-align: center;
  margin-bottom: 30px;
}

.user-profile img {
  width: 100px;
  height: 100px;
  border-radius: 50%;
}

.article-list {
  display: grid;
  gap: 20px;
}

.article-item {
  border: 1px solid #eee;
  padding: 15px;
  border-radius: 8px;
  display: flex;
  gap: 20px;
}

.article-item img {
  width: 200px;
  height: 150px;
  object-fit: cover;
  border-radius: 4px;
}

.article-content {
  flex: 1;
}

.article-meta {
  display: flex;
  gap: 15px;
  color: #666;
  font-size: 14px;
  margin: 10px 0;
}

.article-tags {
  display: flex;
  gap: 8px;
}

.tag {
  background: #f0f0f0;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;
}
</style>
